package cn.tedu.store.util;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Map;
 
public class HttpUtil {
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final String Corpid = "ww30de27d5efb6a9db";//替换成自己的企业ID
    public static final String Corpsecret = "svUTeBOxvOq_4PudJhfXYmjqwbHqfSwuQeAPOTjUOyU";//替换成打卡应用的secret
    public static final String t = "Ox6GMcyHCDXKiRgfR2kE_4XIbSN7kjlqtWQ_TOXL0ig";
    public static final String GetToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
    public static final String GetCheckindata = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata";
    public static final String dept ="https://qyapi.weixin.qq.com/cgi-bin/department/list";
    public static final String GetUserlist = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
 
    public static final Integer MAX_NUM = 100;//企业微信打卡每次最多取100条记录
    //外呼接口
    public static final String WH = "https://gateway.deskpro.cn:8443/dmpadapi/";
    public static void main(String[] args) throws Exception {
        System.out.println(get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?Corpid=ww30de27d5efb6a9db&Corpsecret=svUTeBOxvOq_4PudJhfXYmjqwbHqfSwuQeAPOTjUOyU"));
        //   System.out.println( doGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?Corpid=ww30de27d5efb6a9db&Corpsecret=svUTeBOxvOq_4PudJhfXYmjqwbHqfSwuQeAPOTjUOyU"));
    }
    /**
     * get请求
     * @param url
     * @return
     * @throws Exception
     */
    public static String get(String url) throws Exception {
        String content = null;
        URLConnection urlConnection = new URL(url).openConnection();
        HttpURLConnection connection = (HttpURLConnection) urlConnection;
        connection.setRequestMethod("GET");
        //连接
        connection.connect();
        //得到响应码
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader
                    (connection.getInputStream(), StandardCharsets.UTF_8));
            StringBuilder bs = new StringBuilder();
            String l;
            while ((l = bufferedReader.readLine()) != null) {
                bs.append(l).append("\n");
            }
            content = bs.toString();
        }
        return content;
    }
//blog.csdn.net/csdn_ljl/article/details/96425254
//    public static void main(String[] args) throws Exception {
//        String rq = GetToken + "?Corpid=" + Corpid + "&Corpsecret=" + Corpsecret;
//        JSONObject gettokenRs = JSONObject.parseObject(doGet(rq));
//
//        String token = gettokenRs.getString("access_token");//获取token
//
//        JSONObject body = new JSONObject();
//        List<String> idList = new ArrayList<>();
//        //获取所有人员信息
//        JSONObject getuserlistRs = JSONObject.parseObject(doGet(GetUserlist+ "?access_token=" + token+"&department_id=1&fetch_child=1"));
//        JSONArray userlist = getuserlistRs.getJSONArray("userlist");
//        final int ulsize = userlist.size();
//        for (int i = 0; i < ulsize; i++) {
//            JSONObject udata = userlist.getJSONObject(i);
//            String depuserId = udata.getString("userid");
//           String name = udata.getString("name");
//            System.out.println();
//            idList.add(depuserId);
//        }
//        //每100人为1组取数据
//        final int idlsize = idList.size();
//        for (int j = 0 ; j < idlsize; j+= MAX_NUM) {
//            List<String> useridlist = idList.subList(j, j+MAX_NUM>idList.size()?idList.size():j+MAX_NUM);
//
//            body.put("opencheckindatatype", "3");
//            Calendar calendar = Calendar.getInstance();
//            calendar.set(Calendar.HOUR_OF_DAY, 0);
//            calendar.set(Calendar.MINUTE, 0);
//            calendar.set(Calendar.SECOND, 0);
//            calendar.add(Calendar.DATE, -1);
//            body.put("starttime", calendar.getTimeInMillis() / 1000);
//            calendar.add(Calendar.DATE, 2);
//            body.put("endtime", calendar.getTimeInMillis() / 1000);
//            body.put("useridlist", useridlist);
//            JSONObject checkobj = JSONObject.parseObject(doPost(GetCheckindata + "?access_token=" + token, body));//取打卡数据
//            JSONArray checkindata = checkobj.getJSONArray("checkindata");
//            final int ckdsize = checkindata.size();
//            for (int i = 0; i < ckdsize; i++) {
//                JSONObject data = checkindata.getJSONObject(i);
//                String userId = data.getString("userid");
//                String type = data.getString("checkin_type");
//                String location = data.getString("location_detail");//打卡地点
//                String longitude = data.getString("lng");
//                String latitude = data.getString("lat");
//                Long ckTime = data.getLong("checkin_time");//打卡时间毫秒数
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                String dateTime = sdf.format(ckTime * 1000);//毫秒数转换成日期格式
//                System.out.println(dateTime);
//                String sql = "insert into HrCardData (userId,cardType,cardDate,cardTime,addr,longitude,latitude) values ('" + userId + "','" + type + "','" + dateTime.split(" ")[0] + "','" + dateTime.split(" ")[1] + "','" + location + "','" + longitude + "','" + latitude + "')";
//                System.out.println(sql);
//            }
//        }
//
//    }
 
 
    /**
     * get请求
     *
     * @param getUrl
     * @return
     */
    public static String doGet(String getUrl) {
        try {
            URL url = new URL(getUrl);
            //设置连接方式
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            //设置主机连接时间超时时间3000毫秒
            conn.setConnectTimeout(3000);
            //设置读取远程返回数据的时间3000毫秒
            conn.setReadTimeout(3000);
            //发送请求
            conn.connect();
            //获取输入流
            InputStream is = conn.getInputStream();
            //封装输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            //接收读取数据
            StringBuffer sb = new StringBuffer();
 
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
            }
            if (null != br) {
                br.close();
            }
            if (null != is) {
                is.close();
            }
            //关闭连接
            conn.disconnect();
            return sb.toString();
        } catch (Exception e) {
            e.getMessage();
        }
        return "";
    }

    /**
     * 原生http请求
     *
     * @param sendUrl 请求的Url
     * @param body    传入的参数
     * @return
     */
    public static String doPost(String sendUrl, JSONObject body) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        HttpURLConnection conn = null;
        try {
            URL url = new URL(sendUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            //发送POST请求必须设置为true
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //设置连接超时时间和读取超时时间
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(10000);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            //获取输出流
            out = new OutputStreamWriter(conn.getOutputStream());
            out.write(body.toJSONString());
            out.flush();
            out.close();
            //取得输入流，并使用Reader读取
            if (200 == conn.getResponseCode()) {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                    System.out.println(line);
                }
            } else {
                System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        return result.toString();
    }
 
    /**
     * 将map转String
     *
     * @param map
     * @return
     */
    private static String mapToString(Map<String, String> map) {
        StringBuilder sb = new StringBuilder();
        int index = 0;
        for (String i : map.keySet()) {
            index++;
            if (map.size() == index) {
                sb.append(i + "=" + map.get(i));
            } else {
                sb.append(i + "=" + map.get(i) + "&");
            }
        }
        return sb.toString();
    }
 
}